"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : rule
Case Name   : 为视图表创建rule，delete from视图
Create At   : 2023/02/16
Owner       : @kyeleze
Description :
    1.创建表,为表创建视图
    2.为视图创建rule
    3.delete from视图
Expect      :
    1.成功
    2.成功
    3.成功
History     :
"""

import os
import unittest

from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class RuleTest(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.table_name = 't_table_0037'
        self.view_name = 't_view_037'
        self.rule_name = 't_rule_037'

    def test_rule(self):
        text = '--step1:创建表,为表创建视图 expect:成功--'
        self.log.info(text)
        sql_cmd = f'drop table if exists {self.table_name};' \
                  f'create table {self.table_name}(col int);' \
                  f'create view {self.view_name} as ' \
                  f'select * from {self.table_name};'
        self.log.info(sql_cmd)
        result = self.primary_sh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS,
                      result, '执行失败:' + text)
        self.assertIn(self.constant.CREATE_VIEW_SUCCESS_MSG,
                      result, '执行失败:' + text)

        text = '--step2:为视图创建rule expect:成功--'
        self.log.info(text)
        sql_cmd = f'create or replace rule {self.rule_name} ' \
                  f'as on delete to {self.view_name} do instead nothing;'
        self.log.info(sql_cmd)
        result = self.primary_sh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn('CREATE RULE', result, '执行失败:' + text)

        text = '--step3:delete from视图 expect:成功--'
        self.log.info(text)
        sql_cmd = f'delete from {self.view_name};'
        self.log.info(sql_cmd)
        result = self.primary_sh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn('DELETE 0', result, '执行失败:' + text)

    def tearDown(self):
        text = '--清理环境--'
        self.log.info(text)
        result = self.primary_sh.execut_db_sql(f'drop table {self.table_name} '
                                               f'cascade;')
        self.log.info(result)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS,
                      result, '执行失败:' + text)
        self.log.info(f'----{os.path.basename(__file__)} end----')

